package com.xhui.vimarket.zkplus.zul.attr.msg;

import java.util.List;

import lombok.Getter;
import lombok.Setter;

import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zul.Label;
import org.zkoss.zul.Row;
import org.zkoss.zul.Rows;
import org.zkoss.zul.Window;

import com.xhui.vimarket.zkplus.zul.attr.mod.AttrCompConstants;

/**
 * 规则提示界面.
 * 
 * @版权：福富软件 版权所有 (c) 2011
 * @author wuyx
 * @version Revision 1.0.0
 * @see:
 * @创建日期：2011-3-4
 * @功能说明：
 */
public class AttrMsgExt extends Window {
    
    /**
     * serialVersionUID.
     */
    private static final long serialVersionUID = -3918443649761161692L;
    
    /**
     * 结果.
     */
    @Setter
    @Getter
    private List<RetVO>       retVoList;
    
    /**
     * 确定后执行监听.
     */
    @Setter
    @Getter
    private EventListener     submitListener;
    
    /**
     * 取消执行后监听.
     */
    @Setter
    @Getter
    private EventListener     cancelListener;
    
    /**
     * .
     */
    @Setter
    @Getter
    private boolean           result           = true;
    
    /**
     * Rows.
     */
    private Rows              rowsBox;
    
    /**
     * onCreate.
     */
    public void onCreate() {
        // 绑定事件
        this.initListener();
    }
    
    /**
     * 添加事件监听.
     * 
     * @author wuyx
     *         2011-3-4 wuyx
     */
    public void initListener() {
    }
    
    /**
     * 显示规则.
     * 
     * @author wuyx
     *         2011-3-4 wuyx
     */
    public void display() {
        // clear
        if (this.getRowsbox().getChildren() != null) {
            this.getRowsbox().getChildren().clear();
        }
        // display
        if (this.retVoList == null || this.retVoList.size() < 1) {
            this.createMsgRow("提示信息为空，请联系维护人员处理");
        } else {
            for (RetVO retVo : this.retVoList) {
                if (retVo != null && retVo.getDataList() != null) {
                    for (Object oSubRetVo : retVo.getDataList()) {
                        RetVO subRetVo = (RetVO) oSubRetVo;
                        if (!AttrCompConstants.RET_INFO.equals(subRetVo.getResult())) {
                            result = false;
                        }
                        this.createRuleRow(subRetVo);
                    }
                }
            }
        }
    }
    
    /**
     * 生成规则行.
     * 
     * @param retVO
     *            RetVO
     * @author wuyx
     *         2011-3-4 wuyx
     */
    public void createRuleRow(final RetVO retVO) {
        // 结论规则判断
        
        // 生成行
        if (retVO != null && retVO.getRetMsg() != null) {
            final Row titleRow = (Row) Executions.createComponentsDirectly(
                "<row><div><label /></div></row>", null, this.getRowsbox(), null);
            ((Label) titleRow.getFirstChild().getLastChild()).setValue(retVO.getRetMsg());
        }
    }
    
    /**
     * getRowsbox.
     * 
     * @return Rows
     * @author wuyx
     *         2011-3-4 wuyx
     */
    public Rows getRowsbox() {
        if (this.rowsBox == null) {
            this.rowsBox = (Rows) this.getFellow("rowsBox");
        }
        return this.rowsBox;
    }
    
    /**
     * createMsgRow.
     * 
     * @param msg String
     * @return Row
     * @author wuyx
     *         2011-3-4 wuyx
     */
    public Row createMsgRow(final String msg) {
        final String str = "<row><div>" + msg + "<div></row>";
        final Row row = (Row) Executions.createComponentsDirectly(str, null, this.getRowsbox(),
            null);
        return row;
    }
    
    /**
     * doSubmitRs.
     * 
     * @author wuyx
     *         2011-3-4 wuyx
     */
    public void doSubmitRs() {
        
    }
    
    /**
     * onSubmitRs.
     * 
     * @param event
     *            ForwardEvent
     * @throws Exception
     *             Exception
     * @author wuyx
     *         2011-3-4 wuyx
     */
    public void onSubmitRs(final ForwardEvent event) throws Exception {
        if (this.result) {
            // 触发确定回调函数
            if (this.submitListener != null) {
                try {
                    this.submitListener.onEvent(null);
                } catch (final RtManagerException e) {
                    throw e;
                } catch (final Exception e) {
                    throw new RtManagerException(e);
                }
            }
            Events.postEvent(new Event(Events.ON_CLOSE, this));
        } else {
            this.onCancelRs(event);
        }
    }
    
    /**
     * onCancelRs.
     * 
     * @param event
     *            ForwardEvent
     * @throws Exception
     *             Exception
     * @author wuyx
     *         2011-3-4 wuyx
     */
    public void onCancelRs(final ForwardEvent event) throws Exception {
        // 触发回调函数
        if (this.cancelListener != null) {
            try {
                this.cancelListener.onEvent(null);
            } catch (final RtManagerException e) {
                throw e;
            } catch (final Exception e) {
                throw new RtManagerException(e);
            }
        }
        Events.postEvent(new Event(Events.ON_CLOSE, this));
    }
    
}
